<?php
class view {
    private $template;
    private $data = array();

    public function __construct($template = null){
        $this->template = $template;
    }

	public function __get($key) {
		return $this->data[$key];
	}
	
	public function __set($key, $value) {
		$this->data[$key] = $value;
	}

    public function set_data($data){
        $this->data = array_merge($this->data, $data);
    }

    public function get($key) {
		return $this->data[$key];
	}
	
	public function set($key, $value) {
		$this->data[$key] = $value;
        return $this;
	}

    public function render($template = null) {
        
        if(isset($template)){
            $this->template = $template;
        }

        if (file_exists($this->template)) {
			extract($this->data);
			
      		ob_start();
      
	  		require($this->template);
      
	  		$output = ob_get_contents();

      		ob_end_clean();
      		
                return $output;
    	} else {
			trigger_error('Error: Could not load template ' . $this->template . '!');
			exit();				
    	}
	}
}
